﻿using AutoMapper;
using BW.MesProject.BasicData.Domain.基础;
using BW.MesProject.BasicData.Infrastructure;
using BW.MesProject.Public.ErrorCode;
using BW.MesProject.Public.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace BW.MesProject.BasicData.Application
{
    /// <summary>
    /// 消费者服务实现类
    /// 提供消费者的增删改查等业务功能
    /// </summary>
    public class ConsumerServices : IConsumerServices
    {
        private readonly IBaseRepository<Consumermodel> consu;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数，通过依赖注入获取仓储和映射器
        /// </summary>
        /// <param name="consumer">消费者仓储接口</param>
        /// <param name="mapper">AutoMapper映射器</param>
        public ConsumerServices(IBaseRepository<Consumermodel> consumer, IMapper mapper)
        {
            this.consu = consumer;
            this.mapper = mapper;
        }

        /// <summary>
        /// 添加消费者
        /// </summary>
        /// <param name="model">消费者模型</param>
        /// <returns>添加结果</returns>
        public ApiResult<int> AddConsumer(Consumermodel model)
        {
            var models = mapper.Map<Consumermodel>(model);
            models.Id = YitIdHelper.NextId();
            model.IsDeleted = false;
            return new ApiResult<int>()
            {
                Code = ApiEnums.Success,
                Message = "添加成功",
                Data = consu.Add(model)
            };
        }

        /// <summary>
        /// 删除消费者（逻辑删除）
        /// </summary>
        /// <param name="id">消费者ID</param>
        /// <returns>删除结果</returns>
        public ApiResult<int> DeleteConsumer(long id)
        {
            //根据id查询 修改isdeleted为false
            var model = consu.GetValue(x => x.Id == id);
            model.IsDeleted = true;
            return new ApiResult<int>()
            {
                Code = ApiEnums.Success,
                Message = "删除成功",
                Data = consu.Update(model)
            };
        }

        /// <summary>
        /// 获取所有消费者
        /// </summary>
        /// <returns>消费者列表</returns>
        public ApiResult<List<Consumermodel>> GetAllConsumer()
        {
            return new ApiResult<List<Consumermodel>>()
            {
                Code = ApiEnums.Success,
                Message = "查询成功",
                Data = consu.GetAll().ToList()
            };
        }

        /// <summary>
        /// 分页查询消费者
        /// </summary>
        /// <param name="Consumercode">消费者编码（可选）</param>
        /// <param name="Consumername">消费者名称（可选）</param>
        /// <param name="pageindex">页码</param>
        /// <param name="pagesize">每页大小</param>
        /// <returns>分页结果</returns>
        public ApiPaging<List<Consumermodel>> GetConsumer(string? Consumercode, string? Consumername, int pageindex, int pagesize)
        {
            //查询数据
            var list = consu.GetAll().Where(x => x.IsDeleted == false);
            if (Consumercode != null)
            {
                list = list.Where(x => x.Consumercode.Contains(Consumercode));
            }
            if (Consumername != null)
            {
                list = list.Where(x => x.Consumername.Contains(Consumername));
            }
            //分页
            list = list.Skip((pageindex - 1) * pagesize).Take(pagesize);
            return new ApiPaging<List<Consumermodel>>()
            {
                Code = ApiEnums.Success,
                Message = "查询成功",
                Data = list.ToList(),
                TotalCount = list.Count(),
            };
        }

        /// <summary>
        /// 根据ID获取消费者
        /// </summary>
        /// <param name="id">消费者ID</param>
        /// <returns>消费者信息</returns>
        public ApiResult<Consumermodel> GetConsumerById(long id)
        {
            //根据id查询
            var model = consu.GetValue(x => x.Id == id);
            return new ApiResult<Consumermodel>()
            {
                Code = ApiEnums.Success,
                Message = "查询成功",
                Data = model
            };
        }

        /// <summary>
        /// 更新消费者信息
        /// </summary>
        /// <param name="model">消费者模型</param>
        /// <returns>更新结果</returns>
        public ApiResult<int> UpdateConsumer(Consumermodel model)
        {
            return new ApiResult<int>()
            {
                Code = ApiEnums.Success,
                Message = "更新成功",
                Data = consu.Update(model)
            };
        }
    }
}
